package com.lkx.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 安全的时间格式化工具类
 * @email fanqinghui100@126.com
 * @date 2018/12/3 13:35
 */
public class DateUtil {

    private static ThreadLocal<DateFormat> format = new ThreadLocal<>();
    private final static String datePattern = "yyyy-MM-dd HH:mm:ss";

    public static DateFormat getDateFormat() {
        DateFormat df = format.get();
        if (df == null) {
            df = new SimpleDateFormat(datePattern);
            format.set(df);
        }
        return df;
    }

    public static String formatDate(Date date) throws ParseException {
        return getDateFormat().format(date);
    }

    public static Date parse(String strDate) throws ParseException {
        return getDateFormat().parse(strDate);
    }
}